//
//  FirstRootViewController.m
//  我的测试
//
//  Created by eliot on 16/8/8.
//  Copyright © 2016年 aleven. All rights reserved.
//

#import "FirstRootViewController.h"
#import "FirstRootTableViewCell.h"
#import "ScreeningViewController.h"
#import "DynamicDetailViewController.h"
#import "FirstRootViewVCModel.h"
@interface FirstRootViewController ()<UITableViewDelegate, UITableViewDataSource>

/**左边数据源*/
@property (nonatomic, strong) NSMutableArray *leftArr;
/**右边数据源*/
@property (nonatomic, strong) NSMutableArray *rightArr;
/**左边按钮*/
@property (nonatomic, strong) UIButton *leftBtn;
/**右边按钮*/
@property (nonatomic, strong) UIButton *rightBtn;

@property (nonatomic, strong) UITableView *leftTableView;

@property (nonatomic, strong) UITableView *rightTableView;

@property (nonatomic, strong) NSMutableArray *modelArr;

@end

@implementation FirstRootViewController


#pragma Mark - 懒加载

-(NSMutableArray *)modelArr {
    if (!_modelArr) {
        _modelArr = [NSMutableArray array];
    }
    return _modelArr;
}

-(NSMutableArray *)rightArr {
    if (!_rightArr) {
        _rightArr = [NSMutableArray array];
    }
    return _rightArr;
}

-(NSMutableArray *)leftArr {
    if (!_leftArr) {
        _leftArr = [NSMutableArray array];
    }
    return _leftArr;
}


-(UITableView *)leftTableView {
    if (!_leftTableView) {
        _leftTableView = [[UITableView alloc] init];
        _leftTableView.delegate = self;
        _leftTableView.dataSource = self;
        [_leftTableView registerNib:[UINib nibWithNibName:@"FirstRootTableViewCell" bundle:nil] forCellReuseIdentifier:@"FirstRootTableViewCell"];
        _leftTableView.backgroundColor = f7fColor;
        _leftTableView.tableFooterView = [[UIView alloc] init];
        _leftTableView.frame = CGRectMake(0, 0, Swidth, Sheight);
        [self.view addSubview:_leftTableView];
    }
    return _leftTableView;
}

-(UITableView *)rightTableView {
    if (!_rightTableView) {
        _rightTableView = [[UITableView alloc] init];
        _rightTableView.delegate = self;
        _rightTableView.dataSource = self;
        _rightTableView.backgroundColor = f7fColor;
        _rightTableView.tableFooterView = [[UIView alloc] init];
        [_rightTableView registerNib:[UINib nibWithNibName:@"FirstRootTableViewCell" bundle:nil] forCellReuseIdentifier:@"FirstRootTableViewCell"];
        _rightTableView.frame = CGRectMake(0, 0, 0, Sheight);
         [self.view addSubview:_rightTableView];
    }
    return _rightTableView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setTitleView];
    
    
    for (int i = 0; i < 20; i++) {
        FirstRootViewVCModel *model = [[FirstRootViewVCModel alloc] init];
        model.imgUrl = @"1";
        model.name = @"123";
        model.time = @"10.01";
        model.content = @"这是测试UILabel行间距的text。这是测试UILabel行间距的text。\n 这是测试UILabel行间距的text。\n 这是测试UILabel行间距的text。这是测试UILabel行间距的text。这是测试UILabel行间距的text。这是测试UILabel行间距的text。";
        model.destination = @"shanghai";
        model.fromAddress = @"guangzhou";
        model.duration = @"1geyue";
        model.locateAddress = @"aaa";
        model.pointCount = @"120";
        model.quareImgArr = @[@"4", @"5"];
        model.cellHeight = [self heightWithModel:model];
        model.isOpen = NO;
        [self.modelArr addObject:model];
    }
    self.leftTableView.hidden = NO;
    
}

- (CGFloat)heightWithModel:(FirstRootViewVCModel *)model {
    NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:model.content];
    NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle1 setLineSpacing:8];
    [attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [model.content length])];
    [attributedString1 addAttribute:NSFontAttributeName
     
                              value:[UIFont systemFontOfSize:14.0]
     
                              range:NSMakeRange(0, [model.content length])];
    CGFloat wid=Swidth - 26;
    CGSize siz=CGSizeMake(wid, MAXFLOAT);
    
    
    
    CGFloat contenH = [attributedString1 boundingRectWithSize:siz options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height;
    
    CGFloat openH = 0;
    
    
    return  265 + openH + contenH;


}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - titleView

- (void)setTitleView {
     UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 134/414.0 *Swidth, 35)];//allocate titleView
    [titleView setBackgroundColor:[UIColor clearColor]];
    titleView.layer.borderWidth = 1;
    titleView.layer.borderColor = [[UIColor whiteColor] CGColor];
    titleView.layer.cornerRadius = 4;
    titleView.layer.masksToBounds = YES;
    self.leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.leftBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    self.leftBtn.frame = CGRectMake(0, 0, titleView.frame.size.width / 2, 35);
    [self.leftBtn setTitle:@"全部" forState:UIControlStateNormal];
    self.leftBtn.titleLabel.textColor = UnifiedColor;
    [self.leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [titleView addSubview:self.leftBtn];
    self.rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(titleView.frame.size.width / 2, 0, titleView.frame.size.width / 2, 35)];
    [self.leftBtn addTarget:self action:@selector(clickLeftBtn) forControlEvents:UIControlEventTouchUpInside];
    self.rightBtn.backgroundColor = [UIColor clearColor];
    [self.rightBtn setTitle:@"关注" forState:UIControlStateNormal];
    [self.rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.leftBtn.backgroundColor = [UIColor whiteColor];
    [titleView addSubview:self.rightBtn];
    self.rightBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.rightBtn addTarget:self action:@selector(clickRightBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem.titleView sizeToFit];
    self.navigationItem.titleView = titleView;
    UIColor *color = UnifiedColor;
    [self.leftBtn setTitleColor:color forState:UIControlStateNormal];
    
    UIImage *image = [UIImage imageNamed:@"search"];
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [searchBtn addTarget:self action:@selector(clickSearchBtn) forControlEvents:UIControlEventTouchUpInside];
    [searchBtn setBackgroundImage:image forState:UIControlStateNormal];
    searchBtn.frame = CGRectMake(0, 0, 30, 30);
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchBtn];
    self.navigationItem.leftBarButtonItem = leftBarItem;
    
    
    
    UIImage *image2 = [UIImage imageNamed:@"screen"];
    UIButton *screenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [screenBtn setBackgroundImage:image2 forState:UIControlStateNormal];
    screenBtn.frame = CGRectMake(0, 0, 30, 30);
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:screenBtn];
    self.navigationItem.rightBarButtonItem = rightBarItem;
    [screenBtn addTarget:self action:@selector(clickScreenBtn) forControlEvents:UIControlEventTouchUpInside];
    
}

//点击 搜索按钮
- (void)clickSearchBtn {
    
    

}

//点击 搜筛选按钮
- (void)clickScreenBtn {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"筛选" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
//    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"筛选" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        ScreeningViewController *screening = [[ScreeningViewController alloc] init];
//        [self.navigationController pushViewController:screening animated:YES];
//    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"全部" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        ScreeningViewController *screening = [[ScreeningViewController alloc] init];
        [self.navigationController pushViewController:screening animated:YES];
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"只看动态" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"只看结伴" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *action5 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
//    [alert addAction:action1];
    [alert addAction:action2];
    [alert addAction:action3];
    [alert addAction:action4];
    [alert addAction:action5];
    [self presentViewController:alert animated:YES completion:nil];
    
   
    
}


//点击 左边按钮
- (void)clickLeftBtn {
    UIColor *color = UnifiedColor;
    [self.leftBtn setTitleColor:color forState:UIControlStateNormal];
    self.leftBtn.backgroundColor = [UIColor whiteColor];
    [self.rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.rightBtn.backgroundColor = [UIColor clearColor];
}

//点击 右边按钮
- (void)clickRightBtn {
    UIColor *color = UnifiedColor;
    [self.rightBtn setTitleColor:color forState:UIControlStateNormal];
    self.rightBtn.backgroundColor = [UIColor whiteColor];
    [self.leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.leftBtn.backgroundColor = [UIColor clearColor];
}


#pragma mark - tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//    if (tableView == self.leftTableView) {
//        return self.leftArr.count;
//    }else {
//        return self.rightArr.count;
//    }
    return self.modelArr.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FirstRootTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FirstRootTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (tableView == self.leftTableView) {
        
    }else {
    
    }
    NSIndexSet *sectionIndexSet = [NSIndexSet indexSetWithIndex:indexPath.section];
    FirstRootViewVCModel *model = self.modelArr[indexPath.section];
    cell.model = model;
      __weak typeof(self) weakSelf = self;
    cell.extentionBlock = ^(BOOL isOpen){
        
        [weakSelf.leftTableView reloadSections:sectionIndexSet withRowAnimation:UITableViewRowAnimationFade ];
      
    };
    
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }else {
        return 15;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    FirstRootViewVCModel *model = self.modelArr[indexPath.section];
    //if (model.isOpen) {
        return  model.cellHeight + 154;
    //}else {
   //     return  model.cellHeight ;
   // }

   // return 44.0;
}

//-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return 500;
//}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DynamicDetailViewController *vc = [[DynamicDetailViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];


}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
